<?php

/**
 * Manage data of custom_phone table
 * 
 * @property int $custom_phone_id 
 * @property string $name
 * @property string $image image of phonecase
 * @property string $image_representation representation image of phonecase
 * @property string $user_id 
 * @property float $price 
 * @property float $sale 
 * @property int $create_time 
 * @property int $update_time 
 */
class CustomPhone extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{custom_phone}}';
    }

    public function rules() {
	return array(
	    array('name, image, image_representation', 'required', 'on' => 'insert'),
	    array('name, image, image_representation', 'safe', 'on' => 'update'),
	    array('custom_phone_id, create_time, update_time, image, image_representation', 'safe'),
	    array('price, sale', 'numerical')
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	);
    }

    public function getImg($size = '') {
	switch ($size) {
	    case 'thumb' :
		$width = 150;
		$height = 200;
		break;
	    case 'small' :
		$width = 64;
		$height = 64;
		break;
	    case 'large':
		$width = 800;
		$height = 600;
		break;
	    default :
		return UPLOAD_URL . 'customcase/' . $this->image;
	}
	return ImageHelper::thumb($width, $height, 'customcase' . DIRECTORY_SEPARATOR . $this->image);
    }

    public function getImgRepresentation($size = '') {
	switch ($size) {
	    case 'thumb' :
		$width = 200;
		$height = 150;
		break;
	    case 'small' :
		$width = 64;
		$height = 64;
		break;
	    case 'large':
		$width = 800;
		$height = 600;
		break;
	    default :
		return UPLOAD_URL . 'customcase/' . $this->image_representation;
	}
	return ImageHelper::thumb($width, $height, 'customcase' . DIRECTORY_SEPARATOR . $this->image_representation);
    }

    public function search() {
	$criteria = new CDbCriteria;
	$criteria->compare('name', $this->name, TRUE);
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    'update_time' => CSort::SORT_DESC
		),
	    ),
	));
    }

    public function afterDelete() {
	parent::afterDelete();
    }

    public function getPrice() {
	$price = floatval($this->price);
	if ($this->sale) {
	    $sale = floatval($this->sale);
	    $priceAfterSale = floatval($price - ($price * floatval($sale / 100)));
	    return "<span class='price-before-sale'>$" . $price . "</span><span class='price-after-sale'>$" . $priceAfterSale . "</span>";
	} else {
	    return "<span class='price-after-sale'>$" . $price . "</span>";
	}
    }

}
